package com.hzhk.happyrent.ui.dialog;


import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzhk.happyrent.R;
import com.hzhk.happyrent.utils.InputMethodUtils;
import com.hzhk.happyrent.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * maimai_a
 * cn.maitian.app.ui.dialog
 *
 * @Author: fan
 * @Time: 2016/7/20 13:23
 * @Description:
 */
public class LoadingDialog extends BaseDialog {

    @BindView(R.id.iv_progress)
    ImageView iv_progress;
    @BindView(R.id.tv_tip)
    TextView tv_tip;
    private Activity context;
    private AnimationDrawable animationDrawable;

    public LoadingDialog(final Activity context, String tip) {
        super(context, R.style.SimpleDialog);
        setContentView(R.layout.dialog_loading);
        ButterKnife.bind(this, this);

        this.context = context;
        if (!StringUtils.isEmpty(tip)) tv_tip.setText(tip);
        setCanceledOnTouchOutside(false);
        setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
//                    if (!(context instanceof HomeActivity) && !(context instanceof LoginInputActivity) && !(context instanceof ChatSettingActivity)) {
//                        dismiss();
//                        LoadingDialog.this.context.onBackPressed();
//                    }
                    return true;
                }
                return false;
            }
        });
    }

    public void setMessageVisibility(int visible){
        tv_tip.setVisibility(visible);
    }

    @Override
    public void show() {
        super.show();
        iv_progress.setImageResource(R.drawable.pull_to_refresh_third_anim);
        animationDrawable = (AnimationDrawable) iv_progress.getDrawable();
        animationDrawable.start();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (animationDrawable != null && animationDrawable.isRunning()) {
            animationDrawable.stop();
            animationDrawable = null;
        }
        InputMethodUtils.hideSoftInput(context);
    }
}
